home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1995 March / SOFM_Mar1995.bin / mac / SRI / General Interest / Programming / J / Examples / interest.js < prev    next >
Encoding:
Text File  |  1993-12-02  |  5.1 KB  |  175 lines  |  [TEXT/????]

  1.    NB. interest functions
  2.    NB.                                                                          
  3.    NB.   drn accint    intlist    accumulate intlist for duration
  4.    NB.       intexpand intlist    expand modal intlist
  5.    NB.   frq intm      intrate    convert single intrate to modal
  6.    NB.   frq intmodal  intlist    convert intlist to modal rate
  7.    NB.   frq intrep    intlist    expand intlist
  8.    NB.   frq vrep      intlist    expand intlist to discount rates
  9.    NB.   len stretch   list       stretch list to length len
  10.    NB.
  11.    NB.       accpay    dat        accumulate payments
  12.    NB.   opt accndx    dat        accumulate indexed payments.
  13.    NB.
  14.    NB.       amort     dat        calculate amortization table
  15.    NB.       npv       dat        net present value
  16.    NB.       osb       dat        outstanding loan balance
  17.    NB.       ruleof78  term       outstanding balance on rule of 78
  18.    NB.   int vt        term       v to the power t
  19.    NB.                                                                          
  20.    NB. where:
  21.    NB.   <intlist>  is an interest list, e.g. 0.09 5 0.08 3 0.075              
  22.    NB.   <intrate>  is a single interest rate, e.g. 0.075                      
  23.    
  24.    t=. 'accint accndx accpay amort intexpand intm intmodal intrep'
  25.    t=. t,' npv osb ruleof78 stretch vrep vt'
  26.    SCRIPTNAMES=: t
  27.    
  28.    accint=:      */\ @ (1&,) @ (stretch >: @ intexpand)
  29.    intm=:        <: @ ((^%)~ >:)
  30.    intrep=:      '1 intrep y.' : 'intexpand x. intmodal y.'
  31.    stretch=:     [ $ ] , ($ ,: @ {:)
  32.    vrep=:        % @ >: @ intrep 
  33.    
  34.    aa=: a=. ;@:(2&{.@(,&1)&.>)@((~: <.) <;.1 ])
  35.    intmodal=:    *`intm @. (1&>@]"0) (a f.)
  36.    intexpand=:   1&>.@(#~ 1&|.@(1&>)) # (#~ 1&>)
  37.    
  38.    NB. accndx
  39.    accndx=: 0 : 0
  40. NB. accumulate indexed payments
  41. NB. initial payment rate is 1 per annum.                      
  42. NB.    dat=  imm  0=advance, 1=arrears                            
  43. NB.          frq  payment frequency 12=monthly
  44. NB.          int  annual interest rate
  45. NB.          yrs  number of years                                 
  46. NB.          ndx  indexation rate (annual increases)      
  47. NB.    opt=  0   return end year values only (default)           
  48. NB.          1   return all values
  49. NB.
  50. NB. e.g. accndx 1;12;0.10 5 0.09;24;0.025
  51. NB.      = 24 years payments of 1 indexed at 2.5%
  52. NB.        payable monthly in arrears
  53. NB.        interest 10% for 5 years, 9% thereafter
  54. 0 accndx y.
  55. :
  56. $.=. (5=#y.) >@{ ({.;}.) $.
  57. 'imm frq int yrs ndx'
  58. 'mfiyn'=. y.
  59. len=. f*y
  60. n=. intexpand n
  61. n=. y$0,n,y${:n
  62. i=. f intrep i
  63. i=. len$i,len${:i
  64. p=. f#(%f)**/\1+n
  65. j=. */\len$1+i
  66. r=. j*+/\p%m}.1,(<:m)}.j
  67. (len$x.>.(-f){.1)#r
  68. )
  69.    
  70.    NB. accpay
  71.    accpay=: 0 : 0
  72. NB.    dat= imm  0=advance, 1=arrears
  73. NB.         frq  payment frequency 12=monthly
  74. NB.         int  annual interest rate
  75. NB.         pay  payments per annum
  76. NB.
  77. NB. e.g. accpay 1;12;0.10 5 0.09;24#1
  78. NB.      = 24 years payments of 1 payable monthly in arrears
  79. NB.        interest 10% for 5 years, 9% thereafter
  80. $.=. (4=#y.) >@{ ({.;}.) $.
  81. 'imm frq int pay' 
  82. 'mfip' =. y.
  83. len=. $p=. f#p%f
  84. j=. }. len accint f intmodal i
  85. r=. j*+/\p%m}.1,(m-1)}.j
  86. (len$(-f){.1)#r
  87. )
  88.    
  89.    NB. amort
  90.    amort=: 0 : 0
  91. NB. Generates an amortization table for loan of 1.
  92. NB. payments are assumed to be made in arrears.
  93. NB. 
  94. NB. y. has 3 elements:
  95. NB.   frq  =  payment frequency (e.g. 1=annual, 12=monthly)
  96. NB.   int  =  decimal nominal interest rate per annum.
  97. NB.   yrs  =  number of years of loan
  98. NB.
  99. NB. result is a matrix:  pay osb ip pp
  100. NB.   psy  =  level payment necessary to amortize the loan
  101. NB.   osb  =  outstanding balance before each payment
  102. NB.   ip   =  interest portion of each payment
  103. NB.   pp   =  principal portion of each payment
  104. NB.
  105. NB. e.g  amort 12;0.125;25
  106. NB.      = 25 year loan payable monthly at 12.5%
  107. $.=. (3=#y.) >@{ ({.;}.) $.
  108. 'frq int yrs'
  109. 'fiy'=. y.
  110. len=. f*y
  111. i=. <:(>:i)^%f
  112. vn=. */\1,len$%>:i
  113. osb=. osb%{.osb=.|.-.}.vn
  114. pay=. %+/}.vn
  115. pp=. pay-ip=. osb*i
  116. pay,.osb,.ip,.pp
  117. )
  118.    
  119.    NB. npv
  120.    npv=: 0 : 0
  121. NB. Net present values
  122. NB. y. has 4 elements:
  123. NB.   imm  =  0=advance, 1=arrears
  124. NB.   frq  =  payment frequency (e.g. 1=annual, 12=monthly)
  125. NB.   int  =  annual earned interest rate
  126. NB.   pay  =  payments per annum
  127. NB.
  128. NB. e.g. npv 1;12;0.10 5 0.09;24#1
  129. NB.      = 24 years payments of 1 payable monthly in arrears
  130. NB.        interest 10% for 5 years, 9% thereafter
  131. $.=. (4=#y.) >@{ ({.;}.) $.
  132. 'imm frq int pay'
  133. 'mfip'=. y.
  134. len=. $p=. f#p%f
  135. i=. len$i,len${:i=. f intrep i
  136. +/p*m}.1,(<:m)}.*/\len$%>:i
  137. )
  138.    
  139.    NB. osb
  140.    osb=: 0 : 0
  141. NB. Outstanding balances on loan repaid by level instalments
  142. NB. y. = int,yrs
  143. $.=. (2=#y.) >@{ ({.;}.) $.
  144. 'int yrs'
  145. 'iy'=. y.
  146. $.=. >(*i){ ({.;}.) $.
  147. (|. i. >:y)%y
  148. (%{.)|.-.*/\1,y$%>:i
  149. )
  150.    
  151.    NB. ruleof78
  152.    NB. Generates outstanding balances on rule of 78
  153.    ruleof78=:  (% {.) @ (|.@(+/\)@i.@>:)
  154.    
  155.    NB. vt
  156.    vt=: 0 : 0
  157. :
  158. NB. int vt t
  159. NB. v to the power t
  160. NB.   t   =  durations 
  161. NB.   int =  interest rate
  162. NB.
  163. NB. e.g. 0.065 5 0.05 vt 5 6 7 8
  164. NB.      = discount at durations 5 6 7 8
  165. NB.        interest 6.5% for 5 years, 5% thereafter
  166. $.=. >(1<#x.){ ({.;}.) $.
  167. y.^~%>:x.
  168. i=. intexpand x.
  169. len=. >:>.>./t=. ,y.
  170. v=. 1,*/\len$v,len${:v=. %>:i
  171. r=. (w=. <.t) { v
  172. r [ $.=. $. #~ *./0=f=. t-w
  173. ($y.)$r* f^~w{ (}.%}:)v
  174. )           
  175.